package com.nstar.base.controls
{
	import mx.controls.TextInput;
	import com.nstar.base.util.ColorUtils;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.events.FocusEvent; 
	import mx.controls.TextInput;
	import mx.events.FlexEvent;
	import mx.controls.Label;
	
	/**
	 * 带默认提示的输入框
	 * 
	 * @author 李嘉陵
	 * @since 2007-05-31
	 * modified by zhucm , 2007.9.10
	 */
	public class HintTextInput extends TextInput
	{
		public var defaultText:String = ""
		public var defaultTextColor:String = "#AAAAAA"
		
		//是否必须，如果是必须属性，则在输入框前面加上一个红色的*号
		public var required:Boolean = false;
		var lblRequired:Label ;
		
		public function HintTextInput(){
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, setDefaultText);
			this.addEventListener("textChanged", showClearButton);
			this.addEventListener(FocusEvent.FOCUS_IN, this_focusInHandler)
			this.addEventListener(FocusEvent.FOCUS_OUT, this_focusOutHandler)
		}
		
		public function set value(v:String):void {
			this.text = v;
		}
		
		public function get value():String {
			return this.text;
		}
		
		public function setDefaultText(event:Event):void{
			if(text == ""){
				this.text = "";
				this.textField.text = defaultText;
				this.textField.textColor = ColorUtils.convertColor(defaultTextColor);
			}
			//如果是必须属性，在输入框前面加上红色的*号
			if (required){
				lblRequired = new Label();
				lblRequired.text="*";
				lblRequired.setStyle("color",0xff0000);
				lblRequired.width=10;
				lblRequired.height =10;
				
				this.addChild(lblRequired); 				
			}
		}
		
		private function clearButton_clickHandler(event:MouseEvent):void{
			this.text = "";
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		
		private function this_focusInHandler(event:FocusEvent):void{
			if(text == ""){
				this.text = "";
				this.textField.text = "";
				this.textField.textColor = this.getStyle("color");
			}
		}
		
		private function this_focusOutHandler(event:FocusEvent):void{
			setDefaultText(event);
		}
		

				
		private function showClearButton(event:Event):void{
			this.textField.textColor = this.getStyle("color");
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if (lblRequired!=null){
				lblRequired.x=-5;
				lblRequired.y= 5;;	
			}			
		}
	}
}